function reloadTable() {
    try {
        $('#' + gridname).datagrid('reload');
        $('#' + gridname).treegrid('reload');
    } catch(ex) {}
}
function reloadroleList() {
    $('#roleList').datagrid('reload');
}
function getroleListSelected(field) {
    return getSelected(field);
}
function getSelected(field) {
    var row = $('#' + gridname).datagrid('getSelected');
    if (row != null) {
        value = row[field];
    } else {
        value = '';
    }
    return value;
}
function getroleListSelections(field) {
    var ids = [];
    var rows = $('#roleList').datagrid('getSelections');
    for (var i = 0; i < rows.length; i++) {
        ids.push(rows[i][field]);
    }
    ids.join(',');
    return ids;
};
function getSelectRows() {
    return $('#roleList').datagrid('getChecked');
}
function saveHeader() {
    var columnsFields = null;
    var easyextends = false;
    try {
        columnsFields = $('#roleList').datagrid('getColumns');
        easyextends = true;
    } catch(e) {
        columnsFields = $('#roleList').datagrid('getColumnFields');
    }
    var cols = storage.get('roleListhiddenColumns');
    var init = true;
    if (cols) {
        init = false;
    }
    var hiddencolumns = [];
    for (var i = 0; i < columnsFields.length; i++) {
        if (easyextends) {
            hiddencolumns.push({
                field: columnsFields[i].field,
                hidden: columnsFields[i].hidden
            });
        } else {
            var columsDetail = $('#roleList').datagrid("getColumnOption", columnsFields[i]);
            if (init) {
                hiddencolumns.push({
                    field: columsDetail.field,
                    hidden: columsDetail.hidden,
                    visible: (columsDetail.hidden == true ? false: true)
                });
            } else {
                for (var j = 0; j < cols.length; j++) {
                    if (cols[j].field == columsDetail.field) {
                        hiddencolumns.push({
                            field: columsDetail.field,
                            hidden: columsDetail.hidden,
                            visible: cols[j].visible
                        });
                    }
                }
            }
        }
    }
    storage.set('roleListhiddenColumns', JSON.stringify(hiddencolumns));
}
function restoreheader() {
    var cols = storage.get('roleListhiddenColumns');
    if (!cols) return;
    for (var i = 0; i < cols.length; i++) {
        try {
            if (cols.visible != false) $('#roleList').datagrid((cols[i].hidden == true ? 'hideColumn': 'showColumn'), cols[i].field);
        } catch(e) {}
    }
}
function resetheader() {
    var cols = storage.get('roleListhiddenColumns');
    if (!cols) return;
    for (var i = 0; i < cols.length; i++) {
        try {
            $('#roleList').datagrid((cols.visible == false ? 'hideColumn': 'showColumn'), cols[i].field);
        } catch(e) {}
    }
}
function roleListsearch() {
    var queryParams = $('#roleList').datagrid('options').queryParams;
    $('#roleListtb').find('*').each(function() {
        queryParams[$(this).attr('name')] = $(this).val();
    });
    $('#roleList').datagrid({
        url: 'userController.do?datagrid&field=id,userName,userOrgList.tsDepart.departname,realName,userKey,createBy,createDate,updateBy,updateDate,status,',
        pageNumber: 1
    });
}
function dosearch(params) {
    var jsonparams = $.parseJSON(params);
    $('#roleList').datagrid({
        url: 'userController.do?datagrid&field=id,userName,userOrgList.tsDepart.departname,realName,userKey,createBy,createDate,updateBy,updateDate,status,',
        queryParams: jsonparams
    });
}
function roleListsearchbox(value, name) {
    var queryParams = $('#roleList').datagrid('options').queryParams;
    queryParams[name] = value;
    queryParams.searchfield = name;
    $('#roleList').datagrid('reload');
}
$('#roleListsearchbox').searchbox({
    searcher: function(value, name) {
        roleListsearchbox(value, name);
    },
    menu: '#roleListmm',
    prompt: '请输入查询关键字'
});
function EnterPress(e) {
    var e = e || window.event;
    if (e.keyCode == 13) {
        roleListsearch();
    }
}
function searchReset(name) {
    $("#" + name + "tb").find(":input").val("");
    roleListsearch();
} 

$(function() {
    storage = $.localStorage;
    if (!storage) storage = $.cookieStorage;
    $('#roleList').datagrid({
        idField: 'id',
        title: '角色管理',
        url: 'list',
        fit: true,
        loadMsg: '数据加载中...',
        pageSize: 10,
        pagination: true,
        pageList: [10, 20, 30],
        sortName: 'id',
        sortOrder: 'desc',
        rownumbers: true,
        fitColumns: true,
        striped: true,
        showFooter: true,
        frozenColumns: [[]],
        columns: [[{
            field: 'id',
            title: '编号',
            checkbox: true,
        },
        {
            field: 'name',
            title: '角色名'
        },
        {
            field: 'describes',
            title: '角色描述'
        },
        {
            field: 'createName',
            title: '创建人',
            hidden: false,
            sortable: true
        },
        {
            field: 'createTime',
            title: '创建时间',
            hidden: false,
            sortable: true
        },
        {
            field: 'updateBy',
            title: '修改人',
            hidden: true,
            sortable: true
        },
        {
            field: 'updateDate',
            title: '修改时间',
            hidden: true,
            sortable: true
        },
        {
            field: 'status',
            title: '状态',
            sortable: true,
            formatter: function(value, rec, index) {
                if (value == undefined) return '';
                if (value == '1') {
                    return '<span style="color:green">可用</span>';
                }
                if (value == '0') {
                    return '<span style="color:red;">不可用</span>';
                }
            }
        },
        {
            field: 'opt',
            title: '操作',
            width: 100,
            formatter: function(value, rec, index) {
                if (!rec.id) {
                    return '';
                }
                var href = '';
                href += "[<a href='#' onclick=delObj('userController.do?del&id=" + rec.id + "&userName=" + rec.userName + "','roleList')>";
                href += "删除</a>][<a href='#' onclick=delObj('userController.do?del&id=" + rec.id + "&userName=" + rec.userName + "','roleList')>";
                href += "资源分配</a>]";
                return href;
            }
        }]],
        onLoadSuccess: function(data) {
            $("#roleList").datagrid("clearSelections");
        },
        onClickRow: function(rowIndex, rowData) {
            rowid = rowData.id;
            gridname = 'roleList';
        }
    });
    $('#roleList').datagrid('getPager').pagination({
        beforePageText: '',
        afterPageText: '/{pages}',
        displayMsg: '{from}-{to}共 {total}条',
        showPageList: true,
        showRefresh: true
    });
    $('#roleList').datagrid('getPager').pagination({
        onBeforeRefresh: function(pageNumber, pageSize) {
            $(this).pagination('loading');
            $(this).pagination('loaded');
        }
    });
    try {
        restoreheader();
    } catch(ex) {}
});